package com.aps.apspda.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aps.apspda.R;
import com.aps.apspda.activity.CheckActivity;
import com.aps.apspda.activity.DbStartFunctionActivity;
import com.aps.apspda.activity.EndFunctionActivity;
import com.aps.apspda.activity.EqpStatusChangeActivity;
import com.aps.apspda.activity.ModeSelectActivity;
import com.aps.apspda.activity.StartFunctionActivity;
import com.aps.apspda.activity.ThingChangeListActivity;
import com.aps.apspda.adapter.TabIndexGridAdapter;
import com.aps.apspda.callback.StringCommonCallBack;
import com.aps.apspda.dialog.GeneralFragmentDialog;
import com.aps.apspda.entity.TabIndexGridEntity;
import com.aps.apspda.utils.StaticMembers;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class FunctionFragment extends Fragment {

    @BindView(R.id.rvGrid)
    RecyclerView rvGrid;

    private Unbinder unbinder;

    public FunctionFragment() {
        // Required empty public constructor
    }

    public static FunctionFragment createNewInstance() {
        return new FunctionFragment();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_function, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initData();
        return view;
    }

    private void initView() {
        rvGrid.setLayoutManager(new GridLayoutManager(getActivity(), 3));
    }

    private void initData() {
        final List<TabIndexGridEntity> list = new ArrayList<>();
        if (StaticMembers.CUR_USER.getFEATURESCODE().contains("F0")) {
            list.add(new TabIndexGridEntity(R.drawable.start_function, "批次开始操作", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final GeneralFragmentDialog dialog = new GeneralFragmentDialog();
                    dialog.setCallBackAndWhere(new StringCommonCallBack() {
                        @Override
                        public void onCallback(String str) {
                            if (str.toLowerCase().contains("fb") ||
                                    str.toLowerCase().contains("fd")) {
                                Intent intent = new Intent(getActivity(), DbStartFunctionActivity.class);
                                intent.putExtra("eqpID", str);
                                startActivity(intent);
                            } else {
                                Intent intent = new Intent(getActivity(), StartFunctionActivity.class);
                                intent.putExtra("eqpID", str);
                                startActivity(intent);
                            }
                            dialog.dismiss();
                        }
                    }, 1);
                    dialog.show(getFragmentManager(), "eqp_start_dialog");
                }
            }));
        }
        if (StaticMembers.CUR_USER.getFEATURESCODE().contains("F1")) {
            list.add(new TabIndexGridEntity(R.drawable.end_function, "批次结束操作", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final GeneralFragmentDialog dialog = new GeneralFragmentDialog();
                    dialog.setCallBackAndWhere(new StringCommonCallBack() {
                        @Override
                        public void onCallback(String str) {
                            Intent intent = new Intent(getActivity(), EndFunctionActivity.class);
                            intent.putExtra("eqpID", str);
                            startActivityForResult(intent, 1);
                            dialog.dismiss();
                        }
                    }, 2);
                    dialog.show(getFragmentManager(), "eqp_end_dialog");
                }
            }));
        }
        if (StaticMembers.CUR_USER.getFEATURESCODE().contains("F2")) {
            list.add(new TabIndexGridEntity(R.drawable.thing_change, "材料更换", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final GeneralFragmentDialog dialog = new GeneralFragmentDialog();
                    dialog.setCallBackAndWhere(new StringCommonCallBack() {
                        @Override
                        public void onCallback(String str) {
                            Intent intent = new Intent(getActivity(), ThingChangeListActivity.class);
                            intent.putExtra("eqpID", str);
                            startActivity(intent);
                            dialog.dismiss();
                        }
                    }, 3);
                    dialog.show(getFragmentManager(), "eqp_change_dialog");
                }
            }));
        }
        if (StaticMembers.CUR_USER.getFEATURESCODE().contains("F3")) {
            list.add(new TabIndexGridEntity(R.drawable.eqp_status, "更改设备状态", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final GeneralFragmentDialog dialog = new GeneralFragmentDialog();
                    dialog.setCallBackAndWhere(new StringCommonCallBack() {
                        @Override
                        public void onCallback(String str) {
                            Intent intent = new Intent(getActivity(), EqpStatusChangeActivity.class);
                            intent.putExtra("eqpID", str);
                            startActivity(intent);
                            dialog.dismiss();
                        }
                    }, 4);
                    dialog.show(getFragmentManager(), "eqp_status_change_dialog");
                }
            }));
        }
        if (StaticMembers.CUR_USER.getFEATURESCODE().contains("F4")) {
            list.add(new TabIndexGridEntity(R.drawable.db_start, "DB开始操作", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final GeneralFragmentDialog dialog = new GeneralFragmentDialog();
                    dialog.setCallBackAndWhere(new StringCommonCallBack() {
                        @Override
                        public void onCallback(String str) {
                            Intent intent = new Intent(getActivity(), DbStartFunctionActivity.class);
                            intent.putExtra("eqpID", str);
                            startActivity(intent);
                            dialog.dismiss();
                        }
                    }, 5);
                    dialog.show(getFragmentManager(), "eqp_start_dialog");
                }
            }));
        }
        if (StaticMembers.CUR_USER.getFEATURESCODE().contains("F5")) {
            list.add(new TabIndexGridEntity(R.drawable.check, "日检&周检", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final GeneralFragmentDialog dialog = new GeneralFragmentDialog();
                    dialog.setCallBackAndWhere(new StringCommonCallBack() {
                        @Override
                        public void onCallback(String str) {
                            Intent intent = new Intent(getActivity(), CheckActivity.class);
                            intent.putExtra("eqpID", str);
                            startActivity(intent);
                            dialog.dismiss();
                        }
                    }, 5);
                    dialog.show(getFragmentManager(), "check_dialog");
                }
            }));
        }
        if (StaticMembers.CUR_USER.getFEATURESCODE().contains("F6")) {
            list.add(new TabIndexGridEntity(R.drawable.start_function, "模式选择", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final GeneralFragmentDialog dialog = new GeneralFragmentDialog();
                    dialog.setCallBackAndWhere(new StringCommonCallBack() {
                        @Override
                        public void onCallback(String str) {
                                Intent intent = new Intent(getActivity(), ModeSelectActivity.class);
                                intent.putExtra("eqpID", str);
                                startActivity(intent);
                            dialog.dismiss();
                        }
                    }, 5);
                    dialog.show(getFragmentManager(), "check_dialog");
                }
            }));
        }
        TabIndexGridAdapter tabIndexGridAdapter = new TabIndexGridAdapter(list);
        tabIndexGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        tabIndexGridAdapter.isFirstOnly(true);
        rvGrid.setAdapter(tabIndexGridAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
